package com.ruoyi.oos.web.controller.overrun;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oos.duty.domain.DutyTeam;
import com.ruoyi.oos.duty.service.IDutyTeamService;
import com.ruoyi.oos.overrun.domain.Dutyyear;
import com.ruoyi.oos.overrun.domain.OverrunRecord;
import com.ruoyi.oos.overrun.service.IOverrunRecordService;

/**
 * 轮值年报表统计Controller
 * 
 * @author bulang
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/overrun/dutyyear")
public class DutyYearController extends OverrunController
{
    private String prefix = "overrun/dutyyear";

    @Autowired
    private IOverrunRecordService overrunRecordService;

    @Autowired
    private IDutyTeamService dutyTeamService;
    
    @RequiresPermissions("overrun:dutyyear:view")
    @GetMapping()
    public String record(ModelMap map)
    {
	    	OverrunRecord overrunRecord = new OverrunRecord();
	    	overrunRecord.setStartDate(DateUtils.dateTimeNow("yyyy"));
	    	overrunRecord.setEndDate(DateUtils.dateTimeNow("yyyy"));
	  		
	  		map.put("data", overrunRecord);
        return prefix + "/dutyyear";
    }

        /**
     * 查询超限记录列表
     */
    @RequiresPermissions("overrun:dutyyear:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OverrunRecord overrunRecord)
    {
        startPage();
        
        List<Dutyyear> list = overrunRecordService.selectDutyYearList(overrunRecord);
        return getDataTable(list);
    }
    
    /**
     * 导出轮值年报列表
     */
    @RequiresPermissions("overrun:dutyyear:export")
    @Log(title = "轮值年报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OverrunRecord overrunRecord)
    {
        List<Dutyyear> list = overrunRecordService.selectDutyYearList(overrunRecord);
        ExcelUtil<Dutyyear> util = new ExcelUtil<Dutyyear>(Dutyyear.class);
        return util.exportExcel(list, "轮值年报表");
    }
    

    /**
     * 跳转到同比图形显示页面
     * @param overrunRecord
     * @param map
     * @return
     */
    @RequiresPermissions("overrun:dutyyear:view")
    @GetMapping("/toTbchart")
    public String toTbchart(OverrunRecord overrunRecord, ModelMap map)
    {
    	String tjdate = overrunRecord.getTjdate();
    	String preYear = DateUtils.addDateStr(tjdate, "yyyy", Calendar.YEAR, -1, "yyyy");
    	overrunRecord.setStartDate(tjdate);
    	overrunRecord.setEndDate(preYear);
    	
    	map.put("data", overrunRecord);
      return prefix + "/tbchart";
    }
    
    /**
     * 同比年报表图形展示数据统计
     * @param overrunRecord
     * @param map
     * @return
     */
    @RequiresPermissions("overrun:dutyyear:view")
    @GetMapping("/tbchart")
    @ResponseBody
    public Map<String, Object> tbchart(OverrunRecord overrunRecord)
    {
    	String startDate = overrunRecord.getStartDate();
    	String endDate = overrunRecord.getEndDate();
    	
    	// 获取第一个日期的数据
    	overrunRecord.setTjdate(startDate);
    	List<OverrunRecord> list1 = overrunRecordService.selectDutyYearDetailList(overrunRecord);
    	// 获取第二个日期的数据
    	overrunRecord.setTjdate(endDate);
    	List<OverrunRecord> list2 = overrunRecordService.selectDutyYearDetailList(overrunRecord);
    	// 获取启用轮值班组数据
    	List<DutyTeam> dutylist = dutyTeamService.selectDutyTeamForEnable();

      return handleChartsData(list1, list2, dutylist, startDate, endDate);
    }
    
    /**
     * 跳转到占比图形显示页面
     * @param overrunRecord
     * @param map
     * @return
     */
    @RequiresPermissions("overrun:dutyyear:view")
    @GetMapping("/toZbchart")
    public String toZbchart(OverrunRecord overrunRecord, ModelMap map)
    {
    	map.put("data", overrunRecord);
      return prefix + "/zbchart";
    }
    
    /**
     * 占比月报表图形展示数据统计
     * @param overrunRecord
     * @param map
     * @return
     */
    @RequiresPermissions("overrun:dutyyear:view")
    @GetMapping("/zbchart")
    @ResponseBody
    public Map<String, Object> zbchart(OverrunRecord overrunRecord)
    {
    	// 获取统计日期的数据
    	List<OverrunRecord> list1 = overrunRecordService.selectDutyYearDetailList(overrunRecord);
    	
    	// 获取启用轮值班组数据
    	List<DutyTeam> dutylist = dutyTeamService.selectDutyTeamForEnable();
    	
    	List<Map<String, Object>> ydata1 = new ArrayList<Map<String, Object>>();
    	List<Map<String, Object>> ydata2 = new ArrayList<Map<String, Object>>();
    	List<String> xdata = new ArrayList<String>();
    	
    	for (DutyTeam team : dutylist) {
    		Long identify = team.getIdentify();
    		Long dutyid = team.getDutyId();
    		xdata.add(team.getName());
    		
    		long data1 = 0;
    		long data2 = 0;
    		for (OverrunRecord record : list1) {
    			if (record.getDutyid() == dutyid && record.getIdentify() == identify) {
    				data1 = record.getTimes();
    				data2 = record.getCounts();
    				break;
    			}
    		}
    		Map<String, Object> map1 = new HashMap<String, Object>();
    		map1.put("name", team.getName());
    		map1.put("value", data1);
    		ydata1.add(map1);
    		
    		Map<String, Object> map2 = new HashMap<String, Object>();
    		map2.put("name", team.getName());
    		map2.put("value", data2);
    		ydata2.add(map2);
    	}
    	
    	Map<String, Object> resMap = new HashMap<String, Object>();
    	resMap.put("xdata", xdata);
    	resMap.put("ydata1", ydata1);
    	resMap.put("ydata2", ydata2);
    	
      return resMap;
    }

}
